package com.lijing.binarySortTree;

import java.util.Objects;

/**
 * @Description TODO
 * @title: Node
 * @Author LiJing
 * @Date: 2021/3/2910:31 上午
 * @Version 1.0
 */
public class Node {
    private int value;
    private Node left;
    private Node right;

    public Node() {
    }

    public Node(int value) {
        this.value = value;
    }

    /**
     * 添加节点，按照二叉排序树的方式添加
     * @param node 待添加的节点
     */
    public void add(Node node){
        if (node == null) {
            return;
        }
        if (node.value < this.value){
            if (this.left == null){
                this.left = node;
            }else {
                this.left.add(node);
            }
        }else {
            if (this.right == null){
                this.right = node;
            }else {
                this.right.add(node);
            }
        }
    }
    /**
     * 中序遍历
     */
    public void infixOrder(){
        if (this.left != null){
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null){
            this.right.infixOrder();
        }
    }

    /**
     * 查找节点的方法
     * @param value 希望删除的节点的值
     * @return 找到返回，找不到返回null
     */
    public Node search(int value){
        if (value == this.value){
            return this;
        }else if (value < this.value){
            if (this.left != null){
                return this.left.search(value);
            }else {
                return null;
            }
        }else {
            if (this.right == null){
                return null;
            }
            return this.right.search(value);
        }
    }

    /**
     * 查找待查找节点的父节点
     * @param value 要查找的节点的值
     * @return 返回待查找的节点的父节点
     */
    public Node searchParent(int value){
        if ((this.left != null && this.left.value == value)
                ||(this.right != null && this.right.value == value)){
            return this;
        }else {
            if (value < this.value && this.left != null){
                return this.left.searchParent(value);
            }else if (value >= this.value && this.right != null){
                return this.right.searchParent(value);
            }else {
                return null;
            }
        }
    }

    /**
     * 获取当前节点下子树的最小节点，前提是该二叉树是一个顺序二叉树，就是往左子树一直找
     * @return 返回最小的节点
     */
    public Node getMinNode(){
        Node temp = this;
        while (temp.getLeft() != null){
            temp = temp.getLeft();
        }
        return temp;
    }
    /**
     * 获取当前节点下子树的最大节点，前提是该二叉树是一个顺序二叉树，就是往右子树一直找
     * @return 返回最大的节点
     */
    public Node getMaxNode(){
        Node temp = this;
        while (temp.getRight() != null){
            temp = temp.getRight();
        }
        return temp;
    }


    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public Node getLeft() {
        return left;
    }

    public void setLeft(Node left) {
        this.left = left;
    }

    public Node getRight() {
        return right;
    }

    public void setRight(Node right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }

}
